#include "drv_spi_bit.h"
#include <spi-bit-ops.h>
#include "drv_common.h"


#define		SPI_NUM			4


struct spi_port
{
    gpio_type 	*gpio_x;
    rt_uint32_t gpio_pin;
};

struct spi_data
{
		struct spi_port		sclk;
		struct spi_port		mosi;
}spi_datas[SPI_NUM] = {
		[0] = {GPIOA,GPIO_PINS_5,		GPIOA,GPIO_PINS_6},
		[1] = {GPIOB,GPIO_PINS_13,	GPIOB,GPIO_PINS_14},
		[2] = {GPIOB,GPIO_PINS_3,		GPIOB,GPIO_PINS_4},
		[3] = {GPIOB,GPIO_PINS_7,		GPIOB,GPIO_PINS_8},		
};

struct rt_spi_device	spidev[SPI_NUM] = {
		[0].cs_pin = GET_PIN(A,4),
		[1].cs_pin = GET_PIN(B,12),
		[2].cs_pin = GET_PIN(A,15),
		[3].cs_pin = GET_PIN(B,6),
};


struct rt_spi_configuration config = {
		.data_width = 16,
		.mode 			= RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_0,
		.max_hz     = 1 * 1000 * 1000,
};

void set_sclk(void *data, rt_int32_t state)
{
		struct spi_data		*pd = (struct spi_data	*)data;
		state ? gpio_bits_set(pd->sclk.gpio_x,pd->sclk.gpio_pin) : gpio_bits_reset(pd->sclk.gpio_x,pd->sclk.gpio_pin);		
}

void set_mosi(void *data, rt_int32_t state)
{
		struct spi_data		*pd = (struct spi_data	*)data;
		state ? gpio_bits_set(pd->mosi.gpio_x,pd->mosi.gpio_pin) : gpio_bits_reset(pd->mosi.gpio_x,pd->mosi.gpio_pin);
}

void set_miso(void *data, rt_int32_t state)
{
	
}

void tog_sclk(void *data)
{
		RT_ASSERT(data != RT_NULL);
		struct spi_data		*pd = (struct spi_data	*)data;
	
		gpio_output_data_bit_read(pd->sclk.gpio_x,pd->sclk.gpio_pin) ? gpio_bits_reset(pd->sclk.gpio_x,pd->sclk.gpio_pin) :\
																																	gpio_bits_set(pd->sclk.gpio_x,pd->sclk.gpio_pin);
}






struct rt_spi_bit_ops		_ops={
		.data = RT_NULL,
		.tog_sclk = tog_sclk,
		.set_sclk = set_sclk,
		.set_mosi = set_mosi,
		.set_miso = set_miso,
		.udelay = rt_hw_us_delay
};













































